package com.sf.dlb.controller;


import com.google.gson.Gson;
import com.sf.dlb.bean.SmsRquestBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.service.SmsInfoService;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 短信
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping(value = "/v1/sms")
public class SmsController {
    private static Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    public SmsInfoService smsInfoService;

    /**
     * 获取验证码
     *
     * @return
     */
    @PostMapping(value = "/getVerificationCode")
    @ResponseBody
    public Object getVerificationCode(@RequestBody SmsRquestBean smsRquestBean) {
        if(null==smsRquestBean){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("getVerificationCode request json " + GsonUtils.getInstance().toJson(smsRquestBean));
        if(StringUtils.isEmpty(smsRquestBean.getMobile())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "手机号不能为空");
        }
        ResultVO resultVO=smsInfoService.getVerificationCode(smsRquestBean);
        logger.info("getVerificationCode response json " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 校验验证码
     *
     * @return
     */
    @PostMapping(value = "/checkoutVerificationCode")
    @ResponseBody
    public Object checkoutVerificationCode(@RequestBody SmsRquestBean smsRquestBean) {
        if(null==smsRquestBean){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("checkoutVerificationCode request json=== " + GsonUtils.getInstance().toJson(smsRquestBean));
        if(StringUtils.isEmpty(smsRquestBean.getMobile())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "手机号不能为空");
        }
        if(StringUtils.isEmpty(smsRquestBean.getValidationCode())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "验证码不能为空");
        }
        ResultVO resultVO=smsInfoService.checkoutVerificationCode(smsRquestBean);
        logger.info("checkoutVerificationCode response json=== " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
